package cn.kc.jdbc.service;

import cn.kc.jdbc.dao.ClassDao;
import cn.kc.jdbc.dao.ClassDaoImpl;
import cn.kc.jdbc.dao.StudentDao;
import cn.kc.jdbc.dao.StudentDaoImpl;
import cn.kc.jdbc.entity.Clazz;
import cn.kc.jdbc.entity.Student;

import java.util.List;

/**
 * @author mosin
 * date 2022/2/14
 * @version 1.0
 */
public class StudentServiceImpl  implements  StudentService {

    private StudentDao studentDao = new StudentDaoImpl();
    private ClassDao classDao = new ClassDaoImpl();

    @Override
    public Boolean addStudent(Student student) {

       int line =  studentDao.insert(student);

        return line>0;
    }

    @Override
    public Boolean delStudent(Integer id) {
        int line = studentDao.delById(id);
        return line>0;
    }

    @Override
    public Boolean updateStudent(Integer stuId, String className) {
        /**
         *
         *stuId  学号  className  班级名称
         * 1.根据班级名称 查询班级编号
         *
         * select id  from class  where  className = ?  Class
         *
         * update stu set class_id = ? where  stu_id = ?
         */
        Clazz clazz = classDao.findClazzByClassName(className);
        int line = studentDao.updateStudent(stuId,clazz.getId());

        return line >0;
    }

    @Override
    public List<Student> fidAll() {
        return studentDao.findAll();
    }
}
